<script setup lang="ts">
/*
 * @description:列表 商品表
 * @author: lxx
 * @date: 2025-7-29
 * @version: V1.0.0
 */
import { dataPageApi, dataBatchDelApi, addStockApi } from '@/api/goods'
import type { ItemFormInt } from '@/api/goods/types'
import AddEditForm from './AddEditForm.vue'
import { usePage } from '@/hooks/system/useFramework'
import constants from '@/common/constant'
const router = useRouter()
/* 查询参数*/
const queryParams = ref({
  keywords: '',
  goodType: ''
})
/* 查询的FormRef*/
const queryFormRef = ref()
/* 新增编辑FormRef*/
const addEditFormRef = ref()
const { queryPage, tableData, loading, handleQuery, handleResetQuery, handleAddEdit, handleDelete, handleSelectChange } = usePage<ItemFormInt>(
  dataPageApi,
  dataBatchDelApi,
  queryParams,
  queryFormRef,
  addEditFormRef
)

// 增加库存
const handleAddStock = (row: ItemFormInt) => {
  ElMessageBox.prompt(`请输入数量`, `增加【${row.goodName}】库存`, {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    inputPattern:
      /^\d+$/,
    inputErrorMessage: '请输入正确的数量',
  })
    .then(({ value }) => {
      addStockApi({ goodsId: row.id as Number, goodsNum: Number(value) }).then(() => {
        ElMessage.success('操作成功')
        handleResetQuery()
      })
    })
    .catch(() => {
      // ElMessage({
      //   type: 'info',
      //   message: 'Input canceled',
      // })
    })
}
</script>
<template>
  <div class="web-container-box flex-start-start column">
    <!-- 搜索区域 -->
    <div class="search-container">
      <el-card shadow="hover">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item label="关键字" prop="keywords">
            <el-input v-model="queryParams.keywords" placeholder="关键字" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="商品类型">
            <Dict
              :code="constants.GOODS_TYPE"
              v-model="queryParams.goodType"
              placeholder="请选择商品类型"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item class="search-buttons">
            <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
            <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
            <el-button type="success" icon="plus" @click="handleAddEdit('add')">新增</el-button>
            <el-button type="danger" icon="delete" @click="handleDelete()">删除</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <!-- 列表 -->
    <el-card shadow="hover" class="data-table">
      <el-table fit v-loading="loading" width="100%" highlight-current-row :data="tableData" border @selection-change="handleSelectChange">
        <el-table-column type="selection" width="55" align="center" />

        <el-table-column label="商品名称" prop="goodName" />
        <el-table-column label="商品类型" prop="goodType">
          <template #default="scope">
            <dict-label :code="constants.GOODS_TYPE" :model-value="scope.row.goodType" />
          </template>
        </el-table-column>
        <el-table-column label="单位" prop="goodUnit">
          <template #default="scope">
            <dict-label :code="constants.GOODS_UNIT" :model-value="scope.row.goodUnit" />
          </template>
        </el-table-column>
        <el-table-column label="库存数量" prop="goodNum" />
        <el-table-column label="价格" prop="goodPrice" />
        <el-table-column label="成本价" prop="goodCostPrice" />
        <!-- <el-table-column label="商品图片" prop="goodImgs" /> -->
        <el-table-column label="商品型号" prop="goodModel" />
        
        <el-table-column label="规格" prop="goodSpecs" />
        <el-table-column fixed="right" label="操作" align="center" width="230">
          <template #default="scope">
            <el-button type="success" link size="small" icon="plus" @click.stop="handleAddStock(scope.row)">增加库存</el-button>
            <el-button type="primary" link size="small" icon="edit" @click.stop="handleAddEdit('edit', scope.row)">编辑</el-button>
            <el-button type="danger" link size="small" icon="delete" @click.stop="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-if="queryPage.total > 0"
        layout="prev, pager, next, sizes, total"
        :total="queryPage.total"
        v-model:current-page="queryPage.pageNum"
        v-model:page-size="queryPage.pageSize"
        @current-change="handleQuery"
        @size-change="handleQuery"
      />
    </el-card>
    <!-- 新增、编辑弹窗 -->
    <AddEditForm ref="addEditFormRef"></AddEditForm>
  </div>
</template>
<style lang="scss" scoped></style>
